var Browser = new Object();
Browser.isMozilla = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined') && (typeof HTMLDocument != 'undefined');
Browser.isIE = window.ActiveXObject ? true : false;
Browser.isFirefox = (navigator.userAgent.toLowerCase().indexOf("firefox") != - 1);
Browser.isSafari = (navigator.userAgent.toLowerCase().indexOf("safari") != - 1);
Browser.isOpera = (navigator.userAgent.toLowerCase().indexOf("opera") != - 1);

/*if(document.getElementById("CalFrame")==null){
    document.write("<IFRAME id=CalFrame style='DISPLAY: none; Z-INDEX: 100; LEFT: 0px; WIDTH: 148px; POSITION: absolute; TOP: 0px; HEIGHT: 194px' marginWidth=0 marginHeight=0 src='/V5/STY7/EN/calendar.htm' frameBorder=0 scrolling=no></IFRAME>");
}*/
function writeIframe()
{
    var strIframe = "<html><head><style>"+
    "*{font-size: 10px; font-family: Arial;}"+
    ".bg{color: "+ WebCalendar.lightColor +"; cursor: default; background-color: "+ WebCalendar.darkColor +";}"+
    "table#tableMain{ width: 142px; height: 194px;}"+
    "table#tableWeek td{ color: "+ WebCalendar.lightColor +";}"+
    "table#tableDay  td{ }"+
    "td#ebankYearHead, td#ebankYearMonth{color: "+ WebCalendar.wordColor +"}"+
    ".out { text-align: center; border-top: 1px solid "+ WebCalendar.DarkBorder +"; border-left: 1px solid "+ WebCalendar.DarkBorder +";"+
    "border-right: 1px solid "+ WebCalendar.lightColor +"; border-bottom: 1px solid "+ WebCalendar.lightColor +";}"+
    ".over{ text-align: center; border-top: 1px solid #FFFFFF; border-left: 1px solid #FFFFFF;"+
    "border-bottom: 1px solid "+ WebCalendar.DarkBorder +"; border-right: 1px solid "+ WebCalendar.DarkBorder +"}"+
    "input{ border: 1px solid "+ WebCalendar.darkColor +"; padding-top: 0px; height: 18px; cursor: pointer;"+
    "       color:"+ WebCalendar.wordColor +"; background-color: "+ WebCalendar.btnBgColor +"}"+
    "</style></head><body onselectstart='return false' style='margin: 0px' oncontextmenu='return false'><form name=ebank>";

    if (WebCalendar.drag){ 
        strIframe += "<scr"+"ipt language=javascript>\n"+
    "var drag=false, cx=0, cy=0, o = parent.WebCalendar.calendar;\n"+   
    "document.onkeyup=function(){\n"+
    "   switch(window.event.keyCode){\n"+
    "       case 27 : parent.hiddenCalendar(); break;\n"+
    "       case 37 : parent.prevM(); break; \n"+
    "       case 38 : parent.prevY(); break; \n"+
    "       case 39 : parent.nextM(); break; \n"+
    "       case 40 : parent.nextY(); break;\n"+
    "       case 84 : document.forms[0].today.click(); break;\n"+
    "   } window.event.keyCode = 0; window.event.returnValue= false;}\n"+
    
    "function dragStart(){cx=window.event.clientX; cy=window.event.clientY; drag=true;}</scr"+"ipt>"}

    strIframe += "<select name=tmpMonthSelect onblur='parent.hiddenSelect(this)' style='z-index:1; position:absolute;top:3px;left:18px;display:none'"+
    " onchange='parent.WebCalendar.thisMonth=this.value; parent.hiddenSelect(this); parent.writeCalendar();'></select>"+
    "<select name=tmpYearSelect  onblur='parent.hiddenSelect(this)' style='z-index:1;position:absolute;top:3px;left:74px;display:none'"+
    " onchange='parent.WebCalendar.thisYear =this.value; parent.hiddenSelect(this); parent.writeCalendar();'></select>"+
    
    "<table id=tableMain class=bg border=0 cellspacing=2 cellpadding=0>"+
    "<tr><td width=140 height=19 bgcolor='"+ WebCalendar.lightColor +"'>"+
    "    <table width=140 id=tableHead border=0 cellspacing=1 cellpadding=0><tr align=center>"+
    "    <td width=15 height=19 class=bg title='前一个月' style='cursor: pointer' onclick='parent.prevM()'><b>&lt;</b></td>"+
    
        "    <td width=50 id=ebankYearMonth title='点击这里选择月份' onclick='parent.funMonthSelect(parseInt(this.innerHTML, 10))'"+
    "        onmouseover='this.bgColor=parent.WebCalendar.darkColor; this.style.color=parent.WebCalendar.lightColor'"+
    "        onmouseout='this.bgColor=parent.WebCalendar.lightColor; this.style.color=parent.WebCalendar.wordColor'></td>"+  
    "    <td width=60 id='ebankYearHead'  title='点击这里选择年' onclick='parent.funYearSelect(parseInt(this.innerHTML, 10))'"+
    "        onmouseover='this.bgColor=parent.WebCalendar.darkColor; this.style.color=parent.WebCalendar.lightColor'"+
    "        onmouseout='this.bgColor=parent.WebCalendar.lightColor; this.style.color=parent.WebCalendar.wordColor'></td>"+
    "    <td width=15 class=bg title='下一个月' onclick='parent.nextM()' style='cursor: pointer'><b>&gt;</b></td></tr></table>"+
    "</td></tr><tr><td height=20><table id=tableWeek border=0 width=140 cellpadding=0 cellspacing=0 ";
    if(WebCalendar.drag){strIframe += "onmousedown='dragStart()' onmouseup='drag=false' onmouseout='drag=false'";}
    strIframe += " borderColorLight='"+ WebCalendar.darkColor +"' borderColorDark='"+ WebCalendar.lightColor +"'>"+
    "    <tr align=center><td height=20>日</td><td>一</td><td>二</td><td>三</td><td>四</td><td>五</td><td>六</td></tr></table>"+
    "</td></tr><tr><td valign=top width=140 bgcolor='"+ WebCalendar.lightColor +"'>"+
    "    <table id=tableDay height=120 width=140 border=0 cellspacing=1 cellpadding=0>";
         for(var x=0; x<5; x++){ strIframe += "<tr>";
         for(var y=0; y<7; y++)  strIframe += "<td class=out id='ebankDay"+ (x*7+y) +"'></td>"; strIframe += "</tr>";}
         strIframe += "<tr>";
         for(var x=35; x<39; x++) strIframe += "<td class=out id='ebankDay"+ x +"'></td>";
         strIframe +="<td colspan=3 class=out height=20><input style=' background-color: "+
         WebCalendar.btnBgColor +";cursor: pointer; padding-top: 0px;margin-top: 0px; width: 100%;  border: 0' onfocus='this.blur()'"+
         " type=button value='清空' onclick='parent.emptying()'></td></tr></table>"+
    "</td></tr><tr><td height=20 width=140 bgcolor='"+ WebCalendar.lightColor +"'>"+
    "    <table border='1' cellpadding=1 cellspacing=0 width=140>"+
    "    <tr><td><input style='width: 19px' name=prevYear title='前一年' onclick='parent.prevY()' type=button value='&lt;&lt;'"+
    "    onfocus='this.blur()' style='ebank:expression(this.disabled=parent.WebCalendar.thisYear==1000)'><input  style='width: 19px' "+
    "    onfocus='this.blur()' name=prevMonth title='前一个月' onclick='parent.prevM()' type=button value='&lt;&nbsp;'>"+
    "    </td><td align=center><input name=today type=button value='今天' onfocus='this.blur()' style='width: 50px' title='今天'"+
    "    onclick=\"parent.returnDate(event,new Date().getDate() +'/'+ (new Date().getMonth() +1) +'/'+ new Date().getFullYear())\">"+
    "    </td><td align=right><input  style='width: 19px'  title='下一个月' name=nextMonth onclick='parent.nextM()' type=button value='&nbsp;&gt;'"+
    "    onfocus='this.blur()'><input  style='width: 19px'  name=nextYear title='下一年' onclick='parent.nextY()' type=button value='&gt;&gt;'"+
    "    onfocus='this.blur()' style='ebank:expression(this.disabled=parent.WebCalendar.thisYear==9999)'></td></tr></table>"+
    "</td></tr><table></form></body></html>";
            

    with(WebCalendar.iframe2)
    {
        document.writeln(strIframe); 
        
        document.close();
        for(var i=0; i<39; i++)
        {
            WebCalendar.dayObj[i]=document.getElementById("ebankDay"+i);
            WebCalendar.dayObj[i].onmouseover = dayMouseOver;
            WebCalendar.dayObj[i].onmouseout  = dayMouseOut;
            WebCalendar.dayObj[i].onclick     = returnDate;
        }
    }
    
}

function TWebCalendar() //初始化日历的设置
{
    this.regInfo    = "WEB Calendar ver 3.0&#13;colse:[Esc]";
    this.regInfo   += "&#13;&#13;Ver 2.0";
    this.daysMonth  = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    this.day        = new Array(39);            
    this.dayObj     = new Array(39);            
    this.dateStyle  = null;                     
    this.objExport  = null;                     
    this.eventSrc   = null;                     
    this.inputDate  = null;                     
    this.thisYear   = new Date().getFullYear(); 
    this.thisMonth  = new Date().getMonth()+ 1; 
    this.thisDay    = new Date().getDate();     
    this.today      = this.thisDay +"/"+ this.thisMonth +"/"+ this.thisYear; 
    //this.iframe2     = window.frames["CalFrame"];
    this.iframe2='document.getElementById("CalFrame").contentWindow.document';
    this.calendar   = getObjectById("CalFrame"); 
    this.dateReg    = "";         

    this.yearFall   = 50;         
    this.format2    = "yyyymmdd"; //回传日期的格式
    this.format2Reg = /^(\w{1,4})(-|\/|.|)(\w{1,2})\2(\w{2})$/;
    this.timeShow   = false;
    this.drag       = true;
    this.darkColor  = "#6699CC";
    this.lightColor = "#FFFFFF";
    this.btnBgColor = "#FFFFF5";
    this.wordColor  = "#003399";
    this.wordDark   = "#DCDCDC";
    this.dayBgColor = "#EFF4FB";
    this.todayColor = "#BB2200";
    this.DarkBorder = "#DAE9FF";
    
}   
var WebCalendar = new TWebCalendar();

var isCalendarShowed = false;

function ShowCalendar(eP,v_OutDate,eDP,dmin,dmax,fnCB,posLeft,posTop) 
{
    isCalendarShowed = true;
    WebCalendar.iframe2=document.getElementById("CalFrame").contentWindow;
    WebCalendar.calendar=document.getElementById("CalFrame");
     
    //e=Browser.isIE?window.event.srcElement:e.target;

    writeIframe();
    
    var o = WebCalendar.calendar.style; 
    //WebCalendar.eventSrc = e;

    if (arguments.length == 0 || arguments.length == 1){
	    WebCalendar.objExport = document.getElementById(v_OutDate);
    }else{
		if(typeof(arguments[1])=='object'){
			WebCalendar.objExport=arguments[1];
		}else{
        	WebCalendar.objExport = document.getElementById(arguments[1]);
		}
    }
    
    o.display = ""; WebCalendar.iframe2.document.body.focus();
    
    var eL=0;var eT=0;
    for(var p=eP; p&&p.tagName!='BODY'; p=p.offsetParent){
      eL+=p.offsetLeft;
      eT+=p.offsetTop;
    }
    var eH=eP.offsetHeight;
    var dH=parseInt(o.height);
    var sT=document.body.scrollTop;
    o.left = eL;
    posLeftTmp=0;
    posTopTmp=0;
    if(arguments.length>6){
	posLeftTmp=posLeft;		
    }
    if(arguments.length>7){
	posTopTmp=posTop;
    }
    o.left=eL+posLeftTmp+"px";
    if(eT-dH >= sT && eT+eH+dH > document.body.clientHeight+sT)
	  o.top=eT-dH+posTopTmp+"px";
    else
	    o.top=eT+eH+posTopTmp+"px";	//modified by hyp on 20060814

    if("none"== o.display)
       o.display = "";
    
    if  (!WebCalendar.timeShow) {
    	if(WebCalendar.objExport.value.trim().length==8){
    		WebCalendar.dateReg=/(19|20)\d\d(0|1)\d(0|1|2|3)\d$/;
    	}else WebCalendar.dateReg = /^(\d{1,2})(-|\/|.)(\d{1,2})\2(\d{1,4})$/;
    }
    else WebCalendar.dateReg = /^(\d{1,4})(-|\/|.)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;

    try{
        if (WebCalendar.objExport.value.trim() != ""){
            WebCalendar.dateStyle = WebCalendar.objExport.value.trim().match(WebCalendar.dateReg);
            if (WebCalendar.dateStyle == null)
            {
                WebCalendar.thisYear   = new Date().getFullYear();
                WebCalendar.thisMonth  = new Date().getMonth()+ 1;
                WebCalendar.thisDay    = new Date().getDate();
                writeCalendar(); //return false;
            }
            else
            {
            	var ds = WebCalendar.dateStyle.toString();
            	if(WebCalendar.objExport.value.trim().length==8){
	            	WebCalendar.thisYear   = parseInt(ds.substring(0,4), 10);
	                WebCalendar.thisMonth  = parseInt(ds.substring(4,6), 10);
	                WebCalendar.thisDay    = parseInt(ds.substring(6,8), 10);
	                WebCalendar.inputDate  = parseInt(WebCalendar.thisDay, 10) +"/"+ parseInt(WebCalendar.thisMonth, 10) +"/"+ 
	                parseInt(WebCalendar.thisYear, 10); writeCalendar();	
            	}else{
	            	WebCalendar.thisDay    = parseInt(WebCalendar.dateStyle[1], 10);
	                WebCalendar.thisMonth  = parseInt(WebCalendar.dateStyle[3], 10);
	                WebCalendar.thisYear   = parseInt(WebCalendar.dateStyle[4], 10);
	                WebCalendar.inputDate  = parseInt(WebCalendar.thisDay, 10) +"/"+ parseInt(WebCalendar.thisMonth, 10) +"/"+ 
	                parseInt(WebCalendar.thisYear, 10); writeCalendar();
            	}       
            }
        }  else{
        
         writeCalendar();
         }
    }  catch(e){writeCalendar();}
    
}
var arrMonth = new Array("一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月");
function funMonthSelect()
{
    var m = isNaN(parseInt(WebCalendar.thisMonth, 10)) ? new Date().getMonth() + 1 : parseInt(WebCalendar.thisMonth);
    var e = WebCalendar.iframe2.document.forms[0].tmpMonthSelect;
    for (var i=1; i<13; i++) e.options.add(new Option(arrMonth[i-1], i));
    e.style.display = ""; e.value = m; e.focus(); window.status = e.style.top;
}
function funYearSelect()
{
    var n = WebCalendar.yearFall;
    var e = WebCalendar.iframe2.document.forms[0].tmpYearSelect;
    var y = isNaN(parseInt(WebCalendar.thisYear, 10)) ? new Date().getFullYear() : parseInt(WebCalendar.thisYear);
        y = (y <= 1000)? 1000 : ((y >= 9999)? 9999 : y);
    var min = (y - n >= 1000) ? y - n : 1000;
    var max = (y + n <= 9999) ? y + n : 9999;
        min = (max == 9999) ? max-n*2 : min;
        max = (min == 1000) ? min+n*2 : max;
    for (var i=min; i<=max; i++)
    {
      e.options[e.options.length] = new Option(i +" ", i+"", true, true);//e.options.add(new Option(i +"年", i));
    }
    e.style.display = "";
    e.value = y; e.focus();
}
function prevM()
{
    WebCalendar.thisDay = 1;
    if (WebCalendar.thisMonth==1)
    {
        WebCalendar.thisYear--;
        WebCalendar.thisMonth=13;
    }
    WebCalendar.thisMonth--; writeCalendar();
}
function nextM()
{
    WebCalendar.thisDay = 1;
    if (WebCalendar.thisMonth==12)
    {
        WebCalendar.thisYear++;
        WebCalendar.thisMonth=0;
    }
    WebCalendar.thisMonth++; writeCalendar();
}
function prevY(){WebCalendar.thisDay = 1; WebCalendar.thisYear--; writeCalendar();}
function nextY(){WebCalendar.thisDay = 1; WebCalendar.thisYear++; writeCalendar();}
function hiddenSelect(e){for(var i=e.options.length; i>-1; i--)e.remove(i); e.style.display="none";}
function getObjectById(id){return(document.getElementById(id)); return(eval(id)); }
function hiddenCalendar(){getObjectById("CalFrame").style.display = "none";};
function appendZero(n){return(("00"+ n).substr(("00"+ n).length-2));}
String.prototype.trim=function()
{
    return this.replace(/(^\s*)|(\s*$)/g,"");
}
function dayMouseOver()
{
    this.className = "over";
    this.style.backgroundColor = WebCalendar.darkColor;
    if(WebCalendar.day[this.id.substr(8)].split("/")[1] == WebCalendar.thisMonth)
    this.style.color = WebCalendar.lightColor;
}
function dayMouseOut()
{
    this.className = "out"; var d = WebCalendar.day[this.id.substr(8)], a = d.split("/");

    this.style.backgroundColor='';
    if(a[1] == WebCalendar.thisMonth && d != WebCalendar.today)
    {
        if(WebCalendar.dateStyle && a[0] == parseInt(WebCalendar.dateStyle[4], 10))
        this.style.color = WebCalendar.lightColor;
        this.style.color = WebCalendar.wordColor;
    }
}
function writeCalendar()
{
    var y = WebCalendar.thisYear;
    var m = WebCalendar.thisMonth; 
    var d = WebCalendar.thisDay;
    WebCalendar.daysMonth[1] = (0==y%4 && (y%100!=0 || y%400==0)) ? 29 : 28;
    if (!(y<=9999 && y >= 1000 && parseInt(m, 10)>0 && parseInt(m, 10)<13 && parseInt(d, 10)>0)){
        //alert("The Date inputed is not correct!");
        WebCalendar.thisYear   = new Date().getFullYear();
        WebCalendar.thisMonth  = new Date().getMonth()+ 1;
        WebCalendar.thisDay    = new Date().getDate(); }
    y = WebCalendar.thisYear;
    m = WebCalendar.thisMonth;
    d = WebCalendar.thisDay;
   
    WebCalendar.iframe2.document.getElementById("ebankYearHead").innerHTML  = y +"";
    WebCalendar.iframe2.document.getElementById("ebankYearMonth").innerHTML  = arrMonth[parseInt(m, 10)-1] +"";
    
    WebCalendar.daysMonth[1] = (0==y%4 && (y%100!=0 || y%400==0)) ? 29 : 28;
    var w = new Date(y, m-1, 1).getDay();
    var prevDays = m==1  ? WebCalendar.daysMonth[11] : WebCalendar.daysMonth[m-2];
    for(var i=(w-1); i>=0; i--)
    {
        WebCalendar.day[i] = prevDays +"/"+ (parseInt(m, 10)-1) +"/"+ y;
        if(m==1) WebCalendar.day[i] = prevDays +"/"+ 12 +"/"+ (parseInt(y, 10)-1);
        prevDays--;
    }
    for(var i=1; i<=WebCalendar.daysMonth[m-1]; i++) WebCalendar.day[i+w-1] = i +"/"+ m +"/"+ y;
    for(var i=1; i<39-w-WebCalendar.daysMonth[m-1]+1; i++)
    {
        WebCalendar.day[WebCalendar.daysMonth[m-1]+w-1+i] = i +"/"+ (parseInt(m, 10)+1) +"/"+ y;
        if(m==12) WebCalendar.day[WebCalendar.daysMonth[m-1]+w-1+i] = i +"/"+ 1 +"/"+ (parseInt(y, 10)+1);
    }
    
    for(var i=0; i<39; i++)
    {
        var a = WebCalendar.day[i].split("/");
        WebCalendar.dayObj[i].innerHTML    = a[0];
        WebCalendar.dayObj[i].title        = appendZero(a[0]) +"/"+ appendZero(a[1]) +"/"+ a[2];
        WebCalendar.dayObj[i].bgColor      = WebCalendar.dayBgColor;
        WebCalendar.dayObj[i].style.color  = WebCalendar.wordColor;
        if ((i<10 && parseInt(WebCalendar.day[i], 10)>20) || (i>27 && parseInt(WebCalendar.day[i], 10)<12))
            WebCalendar.dayObj[i].style.color = WebCalendar.wordDark;
        if (WebCalendar.inputDate==WebCalendar.day[i])
        {WebCalendar.dayObj[i].bgColor = WebCalendar.darkColor; WebCalendar.dayObj[i].style.color = WebCalendar.lightColor;}
        if (WebCalendar.day[i] == WebCalendar.today)
        {WebCalendar.dayObj[i].bgColor = WebCalendar.todayColor; WebCalendar.dayObj[i].style.color = WebCalendar.lightColor;}
    }
}
function returnDate()
{

    if(WebCalendar.objExport)
    {
        var returnValue;
	var a;
	if(arguments.length<2){
		a=WebCalendar.day[this.id.substr(8)].split("/");
	}else{
		a=arguments[1].split("/");
	}

        var d = WebCalendar.format2.match(WebCalendar.format2Reg);
        if(d==null){alert("输出日期格式不正确!"); return false;}
        var flag = d[3].length==2 || d[4].length==2;
        returnValue = flag ? a[2] +d[2]+ appendZero(a[1]) +d[2]+ appendZero(a[0]) : a[0] +d[2]+ a[1] +d[2]+ a[2];
        if(WebCalendar.timeShow)
        {
            var h = new Date().getHours(), m = new Date().getMinutes(), s = new Date().getSeconds();
            returnValue += flag ? " "+ appendZero(h) +":"+ appendZero(m) +":"+ appendZero(s) : " "+  h  +":"+ m +":"+ s;
        }
        WebCalendar.objExport.value = returnValue;
        //WebCalendar.objExport.fireEvent("onchange");
        hiddenCalendar();
    }
}


function emptying()
{
    if(WebCalendar.objExport)
    {
        WebCalendar.objExport.value = "";
        hiddenCalendar();
    }
}




document.onclick=function(evt)
{
    if(Browser.isIE){
    	evt = window.event;
    }
    if(isCalendarShowed == true){
      var t = evt.clientX,   l = evt.clientY;
      var o = WebCalendar.calendar.style; 
      if( l < formatPx(o.left) || l > new Number(formatPx(o.left)) + new Number(formatPx(o.width)) || t < formatPx(o.top) || t >  new Number(formatPx(o.top)) +  new Number(formatPx(o.height))) {
    	hiddenCalendar();
      }
   }
}

function formatPx(str){
   if(str != null){
     str = str.substring(0, str.length - 2);	
   }
   return str;	
}
